package com.sjh.baseui.helper

import android.content.Context
import android.graphics.Color

import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator

/**
 *  这是指示器配合ViewPager使用的初始化帮助类，用于简化commonNavigator的初始化
 *  由于项目中出现了很多ViewPager配黑MagicIndicator的翻页界面，每次写都要写一堆配置和CommonNavigatorAdapter实现类
 *  现在把CommonNavigatorAdapter的实现放到一个帮助类里面来实现，配置还是得自己写
 *  调用帮助类的各种属性的setting方法之后，再调用bind方法，完成绑定
 * @author : SunJH
 * @data : 2020/8/12
 */
class NavigatorHelper(
    val context: Context
) {
    companion object {
        fun getInstance(context: Context): NavigatorHelper {
            return NavigatorHelper(context)
        }
    }
    private var mSelectSize = 16
    private var mNormalSize =15
    private var mSelectedColor = Color.WHITE
    private var mNormalColor = Color.WHITE
    private var mode = LinePagerIndicator.MODE_EXACTLY
    private var yoffset = 0f
    private var lineWidth = 0f
    private var lineHeight = 0f
    private var linePagerColor = Color.TRANSPARENT
    private var roundRadius = 0f
    private var isAdjustMod = true
    private var selectIndex:((selectIndex: Int)->Unit)? = null

    fun setColor(selectedColor:Int,normalColor: Int): NavigatorHelper {
        this.mNormalColor = normalColor
        this.mSelectedColor = selectedColor
        return this
    }

    /**
     * 单位dp
     */
    fun setTextSize(selectSize: Int,normalSize:Int): NavigatorHelper {
        this.mSelectSize = selectSize
        this.mNormalSize = normalSize
        return this
    }

    fun setMode(mode: Int): NavigatorHelper {
        this.mode = mode
        return this
    }

    fun setYoffset(yoffset: Float): NavigatorHelper {
        this.yoffset = yoffset
        return this
    }

    fun setLineWidth(lineWidth: Float): NavigatorHelper {
        this.lineWidth = lineWidth
        return this
    }

    fun setLineHeight(lineHeight: Float): NavigatorHelper {
        this.lineHeight = lineHeight
        return this
    }

    fun setLinePagerColor(linePagerColor: Int): NavigatorHelper {
        this.linePagerColor = linePagerColor
        return this
    }

    fun setRoundRadius(roundRadius: Float): NavigatorHelper {
        this.roundRadius = roundRadius
        return this
    }

    fun isAdjustMod(isAdjustMod: Boolean): NavigatorHelper {
        this.isAdjustMod = isAdjustMod
        return this
    }
    fun setSelectHand(hand:((selectIndex:Int)->Unit)): NavigatorHelper {
        this.selectIndex = hand
        return this
    }
    /**
     * 构建magicIndicator的适配器，会使用之前设置的所有参数
     */
    fun build(
        title: ArrayList<String>,
        magicIndicator: MagicIndicator
    ) {
        val commonNavigator = CommonNavigator(context)
        commonNavigator.isAdjustMode = isAdjustMod
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val colorTransitionPagerTitleView = InformationPagerTitleView(context!!)
                colorTransitionPagerTitleView.selectedColor = mSelectedColor
                colorTransitionPagerTitleView.normalColor = mNormalColor
                colorTransitionPagerTitleView.mNormalSize = mNormalSize
                colorTransitionPagerTitleView.mSelectSize = mSelectSize
                colorTransitionPagerTitleView.text = title[index]
                colorTransitionPagerTitleView.setOnClickListener {
                    selectIndex?.invoke(index)
                }
                return colorTransitionPagerTitleView

            }

            override fun getCount(): Int {
                return title.size
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                val linePagerIndicator = LinePagerIndicator(context)
                linePagerIndicator.mode = mode
                linePagerIndicator.yOffset = yoffset
                linePagerIndicator.lineWidth = lineWidth
                linePagerIndicator.lineHeight = lineHeight
                linePagerIndicator.setColors(linePagerColor)
                linePagerIndicator.roundRadius = roundRadius
                return linePagerIndicator
            }

        }
        magicIndicator.navigator = commonNavigator
    }
}